{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Conditional Probability Activity & Exercise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Below is some code to create some fake data on how much stuff people purchase given their age range.\n",
    "\n",
    "It generates 100,000 random \"people\" and randomly assigns them as being in their 20's, 30's, 40's, 50's, 60's, or 70's.\n",
    "\n",
    "It then assigns a lower probability for young people to buy stuff.\n",
    "\n",
    "In the end, we have two Python dictionaries:\n",
    "\n",
    "\"totals\" contains the total number of people in each age group.\n",
    "\"purchases\" contains the total number of things purchased by people in each age group.\n",
    "The grand total of purchases is in totalPurchases, and we know the total number of people is 100,000.\n",
    "\n",
    "Let's run it and have a look:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from numpy import random\n",
    "random.seed(0)\n",
    "\n",
    "totals = {20:0, 30:0, 40:0, 50:0, 60:0, 70:0}\n",
    "purchases = {20:0, 30:0, 40:0, 50:0, 60:0, 70:0}\n",
    "totalPurchases = 0\n",
    "for _ in range(100000):\n",
    "    ageDecade = random.choice([20, 30, 40, 50, 60, 70])\n",
    "    purchaseProbability = float(ageDecade) / 100.0\n",
    "    totals[ageDecade] += 1\n",
    "    if (random.random() < purchaseProbability):\n",
    "        totalPurchases += 1\n",
    "        purchases[ageDecade] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{20: 16576, 30: 16619, 40: 16632, 50: 16805, 60: 16664, 70: 16704}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "totals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{20: 3392, 30: 4974, 40: 6670, 50: 8319, 60: 9944, 70: 11713}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "purchases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45012"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "totalPurchases"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Let's play with conditional probability.\n",
    "\n",
    "First let's compute P(E|F), where E is \"purchase\" and F is \"you're in your 30's\". The probability of someone in their 30's buying something is just the percentage of how many 30-year-olds bought something:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(purchase | 30s): 0.29929598652145134\n"
     ]
    }
   ],
   "source": [
    "PEF = float(purchases[30]) / float(totals[30])\n",
    "print('P(purchase | 30s): ' + str(PEF))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "P(F) is just the probability of being 30 in this data set:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(30's): 0.16619\n"
     ]
    }
   ],
   "source": [
    "PF = float(totals[30]) / 100000.0\n",
    "print(\"P(30's): \" +  str(PF))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "And P(E) is the overall probability of buying something, regardless of your age:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(Purchase):0.45012\n"
     ]
    }
   ],
   "source": [
    "PE = float(totalPurchases) / 100000.0\n",
    "print(\"P(Purchase):\" + str(PE))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "If E and F were independent, then we would expect P(E | F) to be about the same as P(E). But they're not; PE is 0.45, and P(E|F) is 0.3. So, that tells us that E and F are dependent (which we know they are in this example.)\n",
    "\n",
    "What is P(E)P(F)?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(30's)P(Purchase)0.07480544280000001\n"
     ]
    }
   ],
   "source": [
    "print(\"P(30's)P(Purchase)\" + str(PE * PF))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "P(E,F) is different from P(E|F). P(E,F) would be the probability of both being in your 30's and buying something, out of the total population - not just the population of people in their 30's:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(30's, Purchase)0.04974\n"
     ]
    }
   ],
   "source": [
    "print(\"P(30's, Purchase)\" + str(float(purchases[30]) / 100000.0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "P(E,F) = P(E)P(F), and they are pretty close in this example. But because E and F are actually dependent on each other, and the randomness of the data we're working with, it's not quite the same.\n",
    "\n",
    "We can also check that P(E|F) = P(E,F)/P(F) and sure enough, it is:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.29929598652145134\n"
     ]
    }
   ],
   "source": [
    "print((purchases[30] / 100000.0) / PF)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Your Assignment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Modify the code above such that the purchase probability does NOT vary with age, making E and F actually independent.\n",
    "\n",
    "Then, confirm that P(E|F) is about the same as P(E), showing that the conditional probability of purchase for a given age is not any different than the a-priori probability of purchase regardless of age.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
